﻿SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
GO
CREATE PROCEDURE bhp_Temp_ModStructure
AS
BEGIN

	DECLARE
		@ViewFactoryId				INT,
		@PostListModelId			INT,
		@PostModelId					INT,
		@BlogSettingsId				INT,
		@BlogConfigurationId		INT

	SELECT @BlogSettingsId = id FROM n_Tree t WHERE t.name = 'BLOG_SETTINGS' AND parent = 0
	SELECT @ViewFactoryId = id FROM n_Tree t WHERE t.name = 'SYS_BLOG_VIEWFACTORIES'
	SELECT @PostListModelId = id FROM n_Tree t WHERE t.name = 'SYS_BLOG_POSTLISTMODEL'
	SELECT @PostModelId = id FROM n_Tree t WHERE t.name = 'SYS_BLOG_POSTMODEL'

	EXEC [dbo].[n_TableAdd] 'SYS_BLOG_CONFIGURATION', 9999999
		SELECT @BlogConfigurationId = id FROM n_Tree t WHERE t.name = 'SYS_BLOG_CONFIGURATION'

	INSERT INTO bht_Groups (Id, Name, [ReadOnly]) VALUES (@BlogConfigurationId, 'SYS_BLOG_CONFIGURATION', 1)

	DELETE FROM bht_Groups WHERE Id = @ViewFactoryId
	DELETE FROM bht_Groups WHERE Id = @PostListModelId
	DELETE FROM bht_Groups WHERE Id = @PostModelId

	DROP TABLE SYS_BLOG_VIEWFACTORIES
	DROP TABLE SYS_BLOG_POSTLISTMODEL
	DROP TABLE SYS_BLOG_POSTMODEL
	
	ALTER TABLE BLOG_SETTINGS DROP CONSTRAINT
		DF_BLOG_SETTINGS_viewfactory
	
	ALTER TABLE BLOG_SETTINGS DROP CONSTRAINT
		DF_BLOG_SETTINGS_postlistmodel
	
	ALTER TABLE BLOG_SETTINGS DROP CONSTRAINT
		DF_BLOG_SETTINGS_postmodel

	ALTER TABLE BLOG_SETTINGS DROP COLUMN viewfactory
	ALTER TABLE BLOG_SETTINGS DROP COLUMN postlistmodel
	ALTER TABLE BLOG_SETTINGS DROP COLUMN postmodel

	DELETE FROM bht_GroupColumnsSplit WHERE ColumnId IN (SELECT id FROM n_Tree WHERE rel IN(@ViewFactoryId, @PostListModelId, @PostModelId))
	DELETE FROM bht_Columns WHERE ColumnId IN (SELECT id FROM n_Tree WHERE rel IN(@ViewFactoryId, @PostListModelId, @PostModelId))

	DELETE FROM n_Tree WHERE id IN (@ViewFactoryId, @PostListModelId, @PostModelId) OR rel IN (@ViewFactoryId, @PostListModelId, @PostModelId)

	EXEC [dbo].[n_ColFAdd] @rel = @BlogConfigurationId, @name = N'nazwa', @typ = 2, @siz1 = 400, @siz2 = 1, @user = 9999999
	EXEC [dbo].[n_ColFAdd] @rel = @BlogConfigurationId, @name = N'viewfactory', @typ = 2, @siz1 = 400, @siz2 = 1, @user = 9999999
	EXEC [dbo].[n_ColFAdd] @rel = @BlogConfigurationId, @name = N'postlistmodel', @typ = 2, @siz1 = 400, @siz2 = 1, @user = 9999999
	EXEC [dbo].[n_ColFAdd] @rel = @BlogConfigurationId, @name = N'postmodel', @typ = 2, @siz1 = 400, @siz2 = 1, @user = 9999999
	

	EXEC [dbo].[n_ColF1Add] @tab = @BlogSettingsId, @name = N'konfiguracja', @rel = @BlogConfigurationId, @user = 9999999
	
	INSERT INTO bht_Columns (ColumnId, DisplayName, [Description], Sort)
    SELECT
        id, N'Konfiguracja', N'', 600
    FROM
        n_Tree t
    WHERE
        parent = @BlogSettingsId
        AND name = N'konfiguracja'
	
	INSERT INTO bht_GroupColumnsSplit (TabId, ColumnId)
	SELECT
		(SELECT 
			gt.TabId
		FROM 
			n_Tree t
			INNER JOIN bht_Columns c ON t.id = c.ColumnId
			INNER JOIN bht_GroupColumnsSplit gcs ON c.ColumnId = gcs.ColumnId
			INNER JOIN bht_GroupTabs gt ON gcs.TabId = gt.TabId
		WHERE 
			parent = @BlogSettingsId
			AND name = 'title'), id
	FROM
		n_Tree
	WHERE
		parent = @BlogSettingsId
		AND name = 'konfiguracja'

END
GO
EXEC bhp_Temp_ModStructure
GO
DROP PROCEDURE bhp_Temp_ModStructure
GO
INSERT INTO SYS_BLOG_CONFIGURATION (
	del, 
	identyfikator, 
	cuser, 
	ctime, 
	nazwa, 
	viewfactory, 
	postlistmodel, 
	postmodel) VALUES (
	0, --del
	N'Standardowa konfiguracja', --identyfikator
	9999999, --cuser 
	GETDATE(), --ctime
	N'Standardowa konfiguracja', --nazwa
	N'blackhouse.NemoBlog.Web.StandardBlogViewFactory, blackhouse.NemoBlog.Web', --viewfactory
	N'blackhouse.NemoBlog.Business.Model.StandardPostListModel,blackhouse.NemoBlog.Business', --postlistmodel
	N'blackhouse.NemoBlog.Business.Model.StandardPostModel,blackhouse.NemoBlog.Business') --postmodel
GO
ALTER TABLE BLOG_SETTINGS ADD CONSTRAINT
	DF_BLOG_SETTINGS_konfiguracja DEFAULT 1 FOR konfiguracja
GO
UPDATE
	BLOG_SETTINGS
SET
	konfiguracja = 1
GO
ALTER TABLE BLOG_SETTINGS ALTER COLUMN konfiguracja INT NOT NULL
